package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.service.LoginMobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;


/**
 * 类名称: loginMobileController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/7/1 上午 11:53
 * Version 1.0.0
 */
@RestController
@RequestMapping("/login")
public class LoginMobileController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private LoginMobileService loginMobileService;

    @PostMapping("/check")
    public Result check(@RequestBody HashMap<String,String> loginInfo , HttpServletResponse response){
        //得到jedis对象
        Jedis resource = jedisPool.getResource();
        //redis存入的验证码
        String redisInCode = resource.get(RedisMessageConstant.SENDTYPE_LOGIN + "_" + loginInfo.get("telephone"));
        //判断redis里面有没有验证码
        if (null == redisInCode) {
            return new Result(false, "请先通过验证");
        }
        //判断验证码是否与redis里的验证码相同
        if (!redisInCode.equals(loginInfo.get("validateCode"))) {
            return new Result(false, "验证码不正确");
        }
      loginMobileService.addMember(loginInfo);
        Cookie cookie = new Cookie("telephone", loginInfo.get("telephone"));
        response.addCookie(cookie);
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }
}
